import frappe

def delete_sync_logs_for_document(doc, method):
    """
    在删除文档前，删除所有与该文档相关的同步日志。
    
    :param doc: 被删除的文档对象
    :param method: 被触发的钩子方法（如 'before_delete'）
    """
    # frappe.msgprint(f"delete_sync_logs_for_document 已被触发: {doc.name}")

    # 获取当前文档的 doctype 和 docname
    reference_doctype = doc.doctype
    docname = doc.name

    # 查询所有相关的同步日志
    logs = frappe.db.sql("""
        SELECT name
        FROM `tabRB Sync Log`
        WHERE reference_doctype = %s AND docname = %s
    """, (reference_doctype, docname), as_dict=True)
    
    # 如果找到相关的同步日志，则逐条删除
    if logs:
        for log in logs:
            frappe.db.sql("""
                DELETE FROM `tabRB Sync Log`
                WHERE name = %s
            """, (log['name'],))  # 逐条删除
